<?php

class Application_Form_Profile extends Zend_Form
{
    protected $_user;
    
    public function __construct(&$user)
    {
        $this->_user=$user;
        
        parent::__construct();
    }
    
    public function init()
    {
        $this->setAttrib('enctype', 'multipart/form-data');
        $this->setMethod('POST');
        //$this->setAction(Zend_Registry::get('view')->fixUrl('user/edit'));
        
        $nicename=new Zend_Form_Element_Text('nicename');
        $nicename->setLabel('Full name')
              ->addFilter('StringTrim')
              ->addFilter('StripTags')
              ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'min' => 2,
                        'max' => 255
                    )
              )
              ->addDecorator(new Application_Form_ProfileDecorator())
              ->setValue($this->_user->nicename)
              ->setRequired(false);
        
        $password=new Zend_Form_Element_Password('password');
        $password->setLabel('Password')
              ->addFilter('StringTrim')
              ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'min' => 5,
                        'max' => 255
                    )
              )
              ->addDecorator(new Application_Form_ProfileDecorator())
              ->setRequired(false);
              
        $password_re=new Zend_Form_Element_Password('password_re');
        $password_re->setLabel('Retype password')
              ->addFilter('StringTrim')
              ->addValidator(
                    'identical',
                    true,
                    array(
                        'min' => 5,
                        'max' => 255,
                        'token' => 'password'
                    )
              )
              ->addDecorator(new Application_Form_ProfileDecorator())
              ->setRequired(false);
        
        $email=new Zend_Form_Element_Text('email');
        $email->setLabel('E-Mail')
              ->addFilter('StringTrim')
              ->addValidator(
                    'EmailAddress',
                    true
              )
              ->addValidator(
                    new Application_Form_ProfileEmailValidator($this->_user),
                    true
              )
              ->addDecorator(new Application_Form_ProfileDecorator())
              ->setValue($this->_user->email)
              ->setRequired(true);
        
        $selectTypes=array();                 
        $validTypes=Application_Model_UserRoles::getValidRoles();
        foreach ($validTypes as $validType)
        {
            $selectTypes[$validType]=Application_Model_UserRoles::getRoleName($validType);
        }
              
        $role=new Zend_Form_Element_Select('role');
        $role->setLabel('Role')
              ->setMultiOptions($selectTypes)
              ->addDecorator(new Application_Form_ProfileDecorator())
              ->setValue($this->_user->role)
              ->setRequired(true);
              

        $avatar = new Zend_Form_Element_File('avatar');
        $avatar->setLabel('Avatar')
                ->setDestination(ROOT_DIR.'/data/users')
                ->addFilter('Rename',md5($this->_user->id.$this->_user->login.$this->randomChars()))
                ->addValidator('Count', false, 1)
                ->addValidator('Size', false, 512000)      //   500 kB
                ->addValidator('Extension', false, 'jpg,png,gif')
                ->addDecorator(new Application_Form_ProfileDecorator())
                ->setRequired(false)
                ->setValueDisabled(true);   // do not receive file during getValues() call
  
        $about=new Zend_Form_Element_Textarea('description');
        $about->setLabel('About me')
              ->addFilter('StringTrim')
              ->addFilter('StripTags')
              ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'min' => 10,
                        'max' => 1000
                    )
              )
              ->addDecorator(new Application_Form_ProfileDecorator())
              ->setValue(!empty($this->_user->meta['description']) ? $this->_user->meta['description'] : '')
              ->setRequired(false);
              
        $password_current=new Zend_Form_Element_Password('password_current');
        $password_current->setLabel('Current Password')
              ->addFilter('StringTrim')
              ->addValidator(
                    'StringLength',
                    true,
                    array(
                        'min' => 5,
                        'max' => 255
                    )
              )
              ->addValidator(
                    new Application_Form_ProfilePasswordValidator($this->_user),
                    true
              )
              ->addDecorator(new Application_Form_ProfileDecorator())
              ->setRequired(true);
              
         $submit=new Zend_Form_Element_Submit('submit');
         $submit->setValue('Save')
                ->setLabel('')
                ->addDecorator(new Application_Form_ProfileDecorator());
               
         $csrf=new Zend_Form_Element_Hash('csrf',array('salt' => 'userProfile'));
         $csrf->setOptions(array(
                            'ignore' => true,
                        ))
              ->addDecorator(new Application_Form_ProfileDecorator());
              
        $this->addElements(
            array(
                $nicename,
                $password,
                $password_re,
                $email
                )
        );
        
        if (Zend_Registry::get('user')->canManage())
        {
            $this->addElement($role);
        }
        
        $this->addElements(
            array(
                $avatar,
                $about
                )
        );
        
        if (!Zend_Registry::get('user')->canManage())
        {
            $this->addElement($password_current);
        }
        
        $this->addElements(
            array(
                $submit,
                $csrf
                )
        );
    }

        /**
     * Returns a string with random chars
     *
     * @param int $count
     * @return string
     */
    static function randomChars($count=12)
    {
        $random='';
        
        $chars=range('a','z');
        $chars=array_merge($chars,range('A','Z'));
        $chars=array_merge($chars,range(0,9));
        
        while (strlen($random)<$count)
        {
            $random.=$chars[mt_rand(0,count($chars)-1)];
        }
        
        return $random;
    }
}

